package middleware

import (
	"mygame/app/common"
	"mygame/app/common/ret"
	"mygame/app/services"

	"github.com/gin-gonic/gin"
)

type ErrorResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func AuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("Authorization")
		if token == "" {
			ret.ErrorWithCode(c, common.CODE_UNAUTH, "No Token")
			return
		}
		userId, res := services.User{}.CheckToken(token)
		if !res {
			ret.ErrorWithCode(c, common.CODE_UNAUTH, "Unauthorized")
			return
		}
		c.Set("userId", userId)
		c.Next()
	}
}
